package com.example.yls.demob;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ImageView iv_cat;
    private Button btnAnim;
    private MyButton myBtn;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        clickBtn1();
        clickBtn2();
        clickBtn3();
        clickBtn4();
        clickBtn5();

        myBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyColor start = new MyColor();
                start.red = 255;

                MyColor end = new MyColor();
                end.blue = 255;
                ObjectAnimator objectAnimator =
                        ObjectAnimator.ofObject(myBtn, "myColor",
                                new ColorEvaluator(),start, end);
                objectAnimator.setDuration(3000);
                objectAnimator.start();
            }
        });

        btnAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //属性动画ValueAnimator类
                //startPropertyAnimWithJava();
                startPropertyAnimWithXml();
                //使用ValueAnimator.ofObject()
                startPropertyAnimWithObject();
            }
        });
    }

    private void clickBtn5() {
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*//java代码
                AnimatorSet animatorSet = new AnimatorSet();
                float currX = iv_cat.getTranslationX();
                ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(iv_cat,
                        "translationX", currX, 600);
                ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(iv_cat,
                        "scaleX", 1, 5);
                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(iv_cat,
                        "rotation", 360);
                ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(iv_cat,
                        "alpha", 1.0f, 0.2f);
                animatorSet.play(objectAnimator2).with(objectAnimator3).after(objectAnimator1).with(objectAnimator4);
                animatorSet.setDuration(3000);
                animatorSet.start();*/

                //xml实现
                AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator
                        (MainActivity.this, R.animator.anim_set);
                animatorSet.setTarget(iv_cat);
                animatorSet.start();
            }
        });
    }

    private void clickBtn4() {
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnAnim,
                        "alpha", 1.0f, 0.2f);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();*/

                ObjectAnimator objectAnimator = (ObjectAnimator)
                        AnimatorInflater.loadAnimator(MainActivity.this, R.animator.alpha_anim);
                objectAnimator.setTarget(btnAnim);
                objectAnimator.start();
            }
        });
    }

    private void clickBtn3() {
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnAnim,
                        "rotation", 360);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });
    }

    private void clickBtn2() {
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnAnim,
                        "scaleX", 1, 5);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value = (float) valueAnimator.getAnimatedValue();
                        btnAnim.setScaleY(value);
                    }
                });
                objectAnimator.start();
            }
        });
    }

    private void clickBtn1() {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float currX = btnAnim.getTranslationX();
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnAnim,
                        "translationX", currX, 600);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value = (float) valueAnimator.getAnimatedValue();
                        Log.e(TAG, "value = " + value);
                        btnAnim.setTranslationY(value);
                    }
                });
                objectAnimator.start();
            }
        });
    }

    private void startPropertyAnimWithObject() {
        Point start = new Point(150, 100);
        Point end = new Point(500, 700);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(
                new PointEvaluator(), start, end);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Point point = (Point) valueAnimator.getAnimatedValue();
                Log.e(TAG, "x = " + point.getX()+ " y = " + point.getY());
                iv_cat.setTranslationX(point.getX());
                iv_cat.setTranslationY(point.getY());
                //iv_cat.requestLayout();
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.start();
    }

    private void startPropertyAnimWithXml() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(
                MainActivity.this, R.animator.set_animator);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG, "value = " + value);
                btnAnim.getLayoutParams().width = value; //当前按钮的宽度到500dp
                btnAnim.getLayoutParams().height = value;
                btnAnim.setText("属性动画" + value);
                btnAnim.setBackgroundColor(Color.argb(255, value/2, value/3, value));
                btnAnim.requestLayout();  //请求重新布局
            }
        });
        valueAnimator.start();
    }

    private void startPropertyAnimWithJava() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(
                btnAnim.getLayoutParams().width, 500);
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(5);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG, "value = " + value);
                btnAnim.getLayoutParams().width = value; //当前按钮的宽度到500dp
                btnAnim.requestLayout();  //请求重新布局
            }
        });
        valueAnimator.start();
    }

    private void findViews() {
        iv_cat = findViewById(R.id.iv_cat);
        btnAnim = findViewById(R.id.btn_animation);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        myBtn = findViewById(R.id.btn_color);
    }
}
